﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using MySql.Data.MySqlClient;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionLib.Dao
{
    public class DaoOrionUserMenu : DaoBase
    {
        public DaoOrionUserMenu()
            : base()
        {
            this.TableName = "OrionUserMenu";
            this.ViewName = "OrionUserMenu";
        }

        public OrionUserMenu SelectByMenuUser(Int32 OrionMenuLigneId, Int32 OrionUserId)
        {
            lParam.Clear();
            OrionUserMenu retour = new OrionUserMenu();
            String req = this.DebutSelect
                + this.TableName
                + " WHERE OrionUserDroitId=@OrionUserDroitId"
                + " AND OrionMenuDroitId=@OrionMenuDroitId";

            lParam.Add(new MySqlParameter("@OrionUserDroitId", OrionUserId));
            lParam.Add(new MySqlParameter("@OrionMenuDroitId", OrionMenuLigneId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        public OrionUserMenu SelectById(Int32 OrionId)
        {
            lParam.Clear();
            OrionUserMenu retour = new OrionUserMenu();
            String req = this.DebutSelect
                + this.TableName
                + " WHERE OrionId=@orionid";

            lParam.Add(new MySqlParameter("@orionid", OrionId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        public List<OrionUserMenu> SelectAll()
        {
            lParam.Clear();
            List<OrionUserMenu> retour = new List<OrionUserMenu>();
            String req = this.DebutSelect
                + this.TableName;

            MySqlDataReader dr = this.ExecuteQueryReader(req);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();
            return retour;
        }

        public OrionUserMenu Insert(OrionUserMenu om)
        {
            lParam.Clear();

            om.OrionCrea = DateTime.Now;
            om.OrionMaj = DateTime.Now;

            OrionUserMenu retour = new OrionUserMenu();
            String req = "INSERT INTO " + TableName + "(OrionId, OrionUserDroitId, OrionMenuDroitId, OrionCrea, OrionMaj, OrionUserId, OrionEtat) " +
                "VALUES(NULL, @OrionUserDroitId, @OrionMenuDroitId, @OrionCrea, @OrionMaj, @OrionUserId, @OrionEtat)";

            lParam.Add(new MySqlParameter("@OrionUserDroitId", om.OrionUserDroitId));
            lParam.Add(new MySqlParameter("@OrionMenuDroitId", om.OrionMenuDroitId));
            lParam.Add(new MySqlParameter("@OrionCrea", om.OrionCrea));
            lParam.Add(new MySqlParameter("@OrionMaj", om.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionEtat", 1));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(Convert.ToInt32(this.LastInsertId));

            return retour;
        }

        private OrionUserMenu ChargeFromReader(MySqlDataReader dr)
        {
            OrionUserMenu retour = new OrionUserMenu();
            retour.OrionId = dr.GetInt32("OrionId");

            if (dr.IsDBNull(1))
                retour.OrionUserDroitId = 0;
            else
                retour.OrionUserDroitId = dr.GetInt32("OrionUserDroitId");
            if (dr.IsDBNull(2))
                retour.OrionMenuDroitId = 0;
            else
                retour.OrionMenuDroitId = dr.GetInt32("OrionMenuDroitId");

            retour.OrionCrea = dr.GetDateTime("OrionCrea");
            retour.OrionMaj = dr.GetDateTime("OrionMaj");
            retour.OrionUserId = dr.GetInt32("OrionUserId");
            retour.OrionEtat = dr.GetBoolean("OrionEtat");

            return retour;
        }

        public bool IsMenu4User(Int32 orionUserIdP, Int32 orionMenuLigneIdP)
        {
            bool retour = false;
            lParam.Clear();

            String req = this.DebutSelect
                + this.TableName
                + " WHERE OrionUserDroitId=@OrionUserDroitId"
                + " AND OrionMenuDroitId=@OrionMenuDroitId"
                + " AND OrionEtat = 1";

            lParam.Add(new MySqlParameter("@OrionUserDroitId", orionUserIdP));
            lParam.Add(new MySqlParameter("@OrionMenuDroitId", orionMenuLigneIdP));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = true;
            dr.Close();
            return retour;
        }
    }
}
